//
//  API_Recommend.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/21.
//

import UIKit
import Moya

enum API_Recommend {
    /// 猜你喜欢
    case recommend(Int)
    /// 广告接口 1-开屏 2-弹框
    case openAdvert(Int)
    /// 获取订阅信息
    case getSubDetail
    /// 订阅签到
    case checkInSub
}

let recommend_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Recommend>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_recommend = MoyaProvider<API_Recommend>(requestClosure: discover_timeoutClosure)

extension API_Recommend: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .recommend(_):
            return "/Article/bookListSort"
        case .openAdvert(_):
            return "/Advert/OpenAdvert"
        case .getSubDetail:
            return "/Subscription/getSub"
        case .checkInSub:
            return "/Subscription/checkDay"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .recommend(let page):
            ps["page"] = page
            ps["num"] = 18
        case .openAdvert(let type):
            ps["type"] = type
        case .getSubDetail:
            break
        case .checkInSub:
            break
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}
